home *** CD-ROM | disk | FTP | other *** search
- /********************************************************************************
-
- Gnucleus - A node application for the Gnutella network
- Copyright (C) 2000 John Marshall
-
- This program is free software; you can redistribute it and/or modify
- it under the terms of the GNU General Public License as published by
- the Free Software Foundation; either version 2 of the License.
-
- This program is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU General Public License for more details.
-
- You should have received a copy of the GNU General Public License
- along with this program; if not, write to the Free Software
- Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
-
- For support, questions, comments, etc...
- E-Mail:
- swabby@c0re.net
-
- Address:
- 21 Cadogan Way
- Nashua, NH, USA 03062
-
- ********************************************************************************/
-
- // PrefUpload.cpp : implementation file
- //
-
- #include "stdafx.h"
- #include "Gnucleus.h"
- #include "GnucleusDoc.h"
- #include "PrefUpload.h"
-
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
-
- /////////////////////////////////////////////////////////////////////////////
- // CPrefUpload property page
-
- IMPLEMENT_DYNCREATE(CPrefUpload, CPropertyPage)
-
- CPrefUpload::CPrefUpload() : CPropertyPage(CPrefUpload::IDD)
- {
- Doc = NULL;
-
- //{{AFX_DATA_INIT(CPrefUpload)
- //}}AFX_DATA_INIT
- }
-
- CPrefUpload::~CPrefUpload()
- {
- }
-
- void CPrefUpload::DoDataExchange(CDataExchange* pDX)
- {
- CPropertyPage::DoDataExchange(pDX);
- //{{AFX_DATA_MAP(CPrefUpload)
- DDX_Control(pDX, IDC_EDIT_SHARED, m_ebEditShared);
- DDX_Control(pDX, IDC_CHECK_RECURSE, m_chkRecurse);
- DDX_Control(pDX, IDC_CHECK_CLEAR_UL, m_chkClearUL);
- DDX_Control(pDX, IDC_EDIT_MAX_UL, m_ebMaxUL);
- DDX_Control(pDX, IDC_CHECK_MAX_UL, m_chkMaxUL);
- DDX_Control(pDX, IDC_LIST_SHARED, m_lstShared);
- //}}AFX_DATA_MAP
- }
-
-
- BEGIN_MESSAGE_MAP(CPrefUpload, CPropertyPage)
- //{{AFX_MSG_MAP(CPrefUpload)
- ON_BN_CLICKED(IDC_CHECK_MAX_UL, OnCheckMaxUl)
- ON_EN_CHANGE(IDC_EDIT_MAX_UL, OnChangeEditMaxUl)
- ON_BN_CLICKED(IDC_CHECK_CLEAR_UL, OnCheckClearUl)
- ON_BN_CLICKED(IDC_BUTTON_ADD, OnButtonAdd)
- ON_EN_CHANGE(IDC_EDIT_SHAREDIR, OnChangeEditSharedir)
- ON_BN_CLICKED(IDC_BUTTON_REMOVE, OnButtonRemove)
- ON_BN_CLICKED(IDC_BUTTON_BROWSE_SHARE, OnButtonBrowseShare)
- ON_NOTIFY(NM_CLICK, IDC_LIST_SHARED, OnClickListShared)
- //}}AFX_MSG_MAP
- END_MESSAGE_MAP()
-
- /////////////////////////////////////////////////////////////////////////////
- // CPrefUpload message handlers
-
-
- BOOL CPrefUpload::OnInitDialog()
- {
- CPropertyPage::OnInitDialog();
-
- Doc = (CGnucleusDoc *) ((CGnucleusApp *) AfxGetApp())->MasterDoc;
-
- CRect rect;
-
- // speed up apply with a lot of files shared if nothing was changed.
- m_bFilesSharedModified = false;
-
- // Setup shared list
- m_lstShared.GetWindowRect(&rect);
-
- m_lstShared.InsertColumn(0, "Directory", LVCFMT_LEFT,
- (rect.Width() - 30) * 2/3, 0);
- m_lstShared.InsertColumn(1, "File Count", LVCFMT_LEFT,
- (rect.Width() - 30) * 1/3, 1);
-
- m_lstShared.SetExtendedStyle(LVS_EX_FULLROWSELECT);
-
- // Maximum simultaneous uploads
- if(Doc->m_MaxUploads)
- {
- m_chkMaxUL.SetCheck(1);
- m_ebMaxUL.SetWindowText( DWrdtoStr(Doc->m_MaxUploads));
- }
- else
- {
- m_ebMaxUL.SetWindowText("Unlim.");
- m_ebMaxUL.EnableWindow(FALSE);
- }
-
- // Auto clear uploads
- if(Doc->m_AutoClearUL)
- m_chkClearUL.SetCheck(1);
-
- // Load up the listbox
- std::vector<CString>::iterator itDir;
- std::vector<DWORD>::iterator itCount;
-
- for (itDir = Doc->SharedDirList.begin(), itCount = Doc->SharedDirCount.begin();
- itDir != Doc->SharedDirList.end(), itCount != Doc->SharedDirCount.end();
- itDir++, itCount++)
- {
- int row = m_lstShared.GetItemCount();
-
- m_lstShared.InsertItem(row, (*itDir));
- m_lstShared.SetItemText(row, 1, DWrdtoStr((*itCount)));
- }
-
- // Set the recuse check as default
- m_chkRecurse.SetCheck(1);
-
- return TRUE; // return TRUE unless you set the focus to a control
- // EXCEPTION: OCX Property Pages should return FALSE
- }
-
- void CPrefUpload::OnCheckMaxUl()
- {
- if(m_chkMaxUL.GetCheck())
- {
- m_ebMaxUL.SetWindowText( DWrdtoStr(Doc->m_MaxUploads) );
- m_ebMaxUL.EnableWindow();
- }
- else
- {
- m_ebMaxUL.SetWindowText("Unlim.");
- m_ebMaxUL.EnableWindow(FALSE);
- }
-
- SetModified();
- }
-
- void CPrefUpload::OnChangeEditMaxUl()
- {
- SetModified();
- }
-
- void CPrefUpload::OnCheckClearUl()
- {
- SetModified();
- }
-
- void CPrefUpload::OnButtonAdd()
- {
- CFileFind finder;
- CString File;
-
-
- // get updated string
- m_ebEditShared.GetWindowText(File);
-
- if(File.IsEmpty() )
- {
- // call browse routine
- OnButtonBrowseShare();
- m_ebEditShared.GetWindowText(File);
- }
-
- if(!File.IsEmpty() )
- {
- // Simple check to see if the directory actually exists. Will not catch entering a file name.
- SHFILEINFO psfi;
- SHGetFileInfo(File, NULL, &psfi, sizeof(SHFILEINFO), SHGFI_DISPLAYNAME);
- if(strlen(psfi.szDisplayName) )
- {
- // clear edit box,
- m_ebEditShared.Clear();
-
- DWORD FileCount = Recurse(File);
-
- if(m_chkRecurse.GetCheck())
- File += ", Recursive";
-
- int row = m_lstShared.GetItemCount();
-
- // Add the directory to the list box
- m_lstShared.InsertItem(row, File);
-
- // Get the number of files inside the directory
- m_lstShared.SetItemText(row, 1, DWrdtoStr(FileCount));
-
- // clear out of edit box.
- m_ebEditShared.SetWindowText("");
-
- SetModified();
-
- m_bFilesSharedModified = true;
- }
- else
- {
- MessageBox("Please Enter a valid directory!", "Invalid Directory", MB_ICONEXCLAMATION | MB_OK);
- }
- }
- }
-
- DWORD CPrefUpload::Recurse(LPCTSTR pstr)
- {
- CFileFind finder;
-
- DWORD FileCount = 0;
-
- // build a string with wildcards
- CString strWildcard(pstr);
-
- if(strWildcard.GetAt( strWildcard.GetLength() - 1) != '\\')
- strWildcard += _T("\\*.*");
- else
- strWildcard += _T("*.*");
-
- // start working for files
- BOOL bWorking = finder.FindFile(strWildcard);
-
- while (bWorking)
- {
- bWorking = finder.FindNextFile();
-
- // skip . and .. files
- if (finder.IsDots())
- continue;
-
- // if it's a directory, recursively search it
- if (finder.IsDirectory())
- {
- CString str = finder.GetFilePath();
-
- if(m_chkRecurse.GetCheck())
- FileCount += Recurse(str);
- }
- else
- FileCount++;
- }
-
- finder.Close();
-
- return FileCount;
- }
-
-
- void CPrefUpload::OnButtonRemove()
- {
- if(m_lstShared.GetSelectionMark() != -1)
- m_lstShared.DeleteItem( m_lstShared.GetSelectionMark() );
-
- m_bFilesSharedModified = true;
-
- SetModified();
- }
-
- void CPrefUpload::OnChangeEditSharedir()
- {
- SetModified();
- }
-
- //: Browses for new a directory to share, then deposits it into the text box.
- void CPrefUpload::OnButtonBrowseShare()
- {
- // Get the directory to add from the user
- char *Directory = new char[255];
-
- LPBROWSEINFO Settings = new BROWSEINFO;
- Settings->hwndOwner = m_hWnd;
- Settings->pidlRoot = NULL;
- Settings->pszDisplayName = Directory;
- Settings->lpszTitle = "Choose a directory to share...";
- Settings->ulFlags = BIF_RETURNONLYFSDIRS;
- Settings->lpfn = NULL;
- Settings->lParam = NULL;
- Settings->iImage = NULL;
-
- LPCITEMIDLIST FolderID = SHBrowseForFolder(Settings);
- delete Settings;
-
- if(FolderID)
- {
- SHGetPathFromIDList(FolderID, Directory);
- m_ebEditShared.SetWindowText(Directory);
- }
-
- delete [] Directory;
- }
-
-
- BOOL CPrefUpload::OnApply()
- {
- CString store;
-
- // Maximum simultaneous uploads
- if(m_chkMaxUL.GetCheck())
- {
- m_ebMaxUL.GetWindowText(store);
-
- if(store != "")
- Doc->m_MaxUploads = atoi(store);
- }
- else
- Doc->m_MaxUploads = 0;
-
- // Auto clear uploads
- if(m_chkClearUL.GetCheck())
- Doc->m_AutoClearUL = 1;
- else
- Doc->m_AutoClearUL = 0;
-
- // Rebuild the list if modified
- if(m_bFilesSharedModified)
- {
- Doc->LockUploadData (true);
-
- Doc->SharedDirList.clear();
- Doc->SharedDirCount.clear();
- Doc->SharedDirSize.clear();
-
- Doc->SharedFiles.clear();
- Doc->SharedSizes.clear();
-
- for(int row = 0; row < m_lstShared.GetItemCount(); row++)
- {
- Doc->SharedDirList.push_back( m_lstShared.GetItemText(row, 0) );
- Doc->SharedDirCount.push_back(0);
- Doc->SharedDirSize.push_back(0);
- }
-
- Doc->UnlockUploadData (true);
-
- Doc->LoadShare();
-
- m_bFilesSharedModified = false;
- }
-
- return CPropertyPage::OnApply();
- }
-
- void CPrefUpload::OnClickListShared(NMHDR* pNMHDR, LRESULT* pResult)
- {
- *pResult = 0;
- int i_item = 0;
-
- if( (i_item = m_lstShared.GetSelectionMark()) != -1 )
- {
- CString item_text = m_lstShared.GetItemText(i_item, 0);
-
- if( item_text.Find(", Recursive") != -1)
- {
- item_text = item_text.Left(item_text.GetLength() - 11);
- }
-
- m_ebEditShared.SetWindowText(item_text);
- }
-
- }
-